<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录图标--S -->
      <div class="avatar_box">
        <img src="@/assets/logo.svg" alt="登录" />
      </div>
      <!-- 登录图标--E -->
      <!-- 登录表单--S -->
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <!-- 登录 -->
        <el-form-item class="btns">
          <el-button type="primary" class="btns_same" @click="onLogin"
            >登录</el-button
          >
          <el-button type="info" class="btns_same" @click="loginFormReset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 登录表单--E -->
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import { setItem } from '@/utils/storage'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    loginFormReset() {
      this.$refs.loginFormRef.resetFields()
    },
    // 登录
    onLogin() {
      // 表单预验证
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const loading = this.$loading({
          lock: true,
          text: '登录中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        try {
          const { data } = await loginAPI(this.loginForm)
          // console.log(data)
          if (data.meta.status !== 200) {
            loading.close()
            return this.$message({
              message: data.meta.msg,
              type: 'warning',
              center: true,
              offset: 100
            })
          }
          this.$message({
            message: '登录成功',
            type: 'success',
            center: true,
            offset: 100,
            duration: 1000
          })
          setItem('token', data.data.token)
          this.$router.push({
            name: 'home'
          })
        } catch (err) {
          this.$message({
            message: '登录失败，请稍后再试',
            type: 'error',
            center: true,
            offset: 100
          })
        }
        loading.close()
      })
    }
  }
}
</script>

<style scoped lang="less">
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  .login_box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    .avatar_box {
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 130px;
      height: 130px;
      background-color: #fff;
      border: 3px solid #ccc;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ccc;
      img {
        display: block;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #ccc;
      }
    }
    .login_form {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      padding: 20px;
      .btns {
        display: flex;
        justify-content: space-between;
        .btns_same {
          margin: 0 50px;
        }
      }
    }
  }
}
</style>
